﻿using Caliburn.Micro;
using Phones.Client.Model;
using Phones.Client.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Phones.Client.ViewModel
{
    [Export(typeof(RootTreeNodeViewModel))]
    public class RootTreeNodeViewModel : TreeViewItemBaseViewModel
    {
        private string _name = "Зоны обслуживания";
        readonly IWindowManager _windowManager;

        public string Name
        {
            get { return _name; }
            set 
            { 
                _name = value;
                NotifyOfPropertyChange(() => Name);
            }
        }

        [ImportingConstructor]
        public RootTreeNodeViewModel(IWindowManager windowManager)
            : base(null, false)
        {
            _windowManager = windowManager;
            LoadChildren();
            IsExpanded = true;
        }

        protected override void LoadChildren()
        {
            using (var _db = new ServiceNodesRepository())
            {
                foreach (ServiceNode node in _db.GetList())
                {
                    Children.Add(new ServiceNodeTreeNodeViewModel(node));
                }
            }
        }

        #region Actions block
        public void AddServiceNode()
        {
            var node = new ServiceNode();

            var result =
                _windowManager.ShowDialog(
                        new ServiceNodePropertiesViewModel(node)
                    );

            if (result.HasValue && result.Value)
            {
                using (var db = new UnitOfWork<ServiceNode>())
                {
                    if (db.Insert(node) > 0)
                    {
                        Children.Add(new ServiceNodeTreeNodeViewModel(node));
                    }
                }
            }
        }

        public bool CanAddServiceNode()
        {
            return true;
        }

        public void EditServiceNode(object sender)
        {
            var nodeVM = sender as ServiceNodeTreeNodeViewModel;
            if (nodeVM == null)
                throw new ArgumentException(
                    "Недопустимый вызов метода правки сервисного узла.");
            var result =
                _windowManager.ShowDialog(
                        new ServiceNodePropertiesViewModel(nodeVM.Model)
                    );

            if (result.HasValue && result.Value)
            {
                using (var db = new UnitOfWork<ServiceNode>())
                {
                    db.Update(nodeVM.Model);
                }
            }
            else
            {
                using (var db = new UnitOfWork<ServiceNode>())
                {
                    db.Restore(nodeVM.Model);
                }
            }
        }

        public bool CanEditServiceNode(object sender)
        {
            var nodeVM = sender as ServiceNodeTreeNodeViewModel;
            return nodeVM != null;
        }

        public void RemoveServiceNode(object sender)
        {
            var vm = sender as ServiceNodeTreeNodeViewModel;
            if (vm == null)
                throw new ArgumentException(
                    "Недопустимый вызов метода правки сервисного узла.");
            using (var db = new UnitOfWork<ServiceNode>())
            {
                if (db.Delete(vm.Model) > 0)
                {
                    Children.Remove(vm);
                }
            }
        }

        public bool CanRemoveServiceNode(object sender)
        {
            var vm = sender as ServiceNodeTreeNodeViewModel;
            return vm != null && vm.Cities.Count == 0;
        }
        #endregion
    }
}
